// page_one/jiankongpz/jiankongpz.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    heights: "",
    yuming,
    titles: '添加监控',
    buttonClicked: false,
    sbbh: '',
    sbyz: '',
    azqy: '',
    fucrrr: 0,
    dibu_img: '', //底部照片
    wudibu_img: '',
    factoryid: 1, // 1.乐橙，3 萤石云
    // fuwu: [{
    //   operLogo: yuming + 'static/applet/images/fuwu/yingshi.png',
    //   name: '萤石云'
    // }, {
    //   operLogo: yuming + 'static/applet/images/fuwu/lecheng.png',
    //   name: '乐橙'
    // }],
    fuwu: [],
    // area: '',
    // areaList: [{
    //     checked: false,
    //     value: '大厅',
    //   },
    //   {
    //     checked: false,
    //     value: '客厅',
    //   },
    // ],
    beizhu: '',
    fuwu: '', //服务商id
    monitor_id: '', //设备编号
    monitor_code: '', //设备验证码
    monitor_appkey: '', //AppKey
    monitor_appsecret: '', //Secret
    title: "", //安装区域
  },
  // 服务商
  fuwuist() {
    console.log(111111111111111)
    var that = this
    this.myRequest(yuming + 'Monitor/shopSeeMonitor', {
      shopId: wx.getStorageSync('shopId')
    }, 'POST').then(res => {
      console.log(res)
      that.setData({
        fuwu: res.data.data,
        mid: res.data.data[0].id,
      })
    })
  },
  // 上传
  shangchuan(e) {
    var that = this;
    this.myChooseImage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
        var urls = data.httpUrl + data.imgUrl
        that.setData({
          dibu_img: urls,
          wudibu_img: data.imgUrl
        });
      })
    })
  },
  fangda() {
    console.log('点击了放大！')
    wx.previewImage({
      urls: [this.data.dibu_img]
    })
  },
  huan() {
    var that = this
    wx.showModal({
      title: '您想实现哪种操作？',
      cancelText: '放大',
      confirmText: '重新上传',
      success: function (res) {
        if (res.confirm) {
          console.log("重新上传")
          that.shangchuan()
        } else {
          console.log("放大")
          that.fangda()
        }
      }
    })
  },
  // 选择品牌
  xuanfu(e) {
    var index = e.currentTarget.dataset.index;
    var factoryid = e.currentTarget.dataset.factoryid;
    console.log(factoryid)
    this.setData({
      fucrrr: index,
      mid: this.data.fuwu[index].id,
      jine: this.data.fuwu[index].price * this.data.num,
      je: this.data.fuwu[index].price,
      factoryid: factoryid,
    })
  },
  bininput(e) {
    let val = e.detail.value
    let modal = e.currentTarget.dataset.modal
    this.setData({
      [modal]: val
    })
  },
  //多选
  // userCheck: function (e) {
  //   let index = e.currentTarget.dataset.id; //获取用户当前选中的索引值
  //   let Box = this.data.areaList
  //   if (Box[index].checked) {
  //     this.data.areaList[index].checked = false;
  //   } else {
  //     this.data.areaList[index].checked = true;
  //   }
  //   this.setData({
  //     areaList: this.data.areaList
  //   })
  //   // 返回用户选中的值
  //   let value = Box.filter((item, index) => {
  //     return item.checked == true;
  //   })
  //   let xditem = ''
  //   value.forEach((item, index) => {
  //     xditem += item.value + ','
  //   })
  //   if (xditem.slice(-1) == ',') {
  //     xditem = xditem.slice(0, xditem.length - 1);
  //   }
  //   this.setData({
  //     area: xditem
  //   })
  //   console.log(this.data.areaList)
  // },


  // 备注
  beizhu(e) {
    this.setData({
      beizhu: e.detail.value
    })
  },
  tijiao() {
    // 萤石云
    if (this.data.factoryid == 3) {
      if (this.data.monitor_id == '') {
        wx.showToast({
          title: '请输入设备编号',
          icon: 'none'
        })
      } 
      // else if (this.data.title == '') {
      //   wx.showToast({
      //     title: '请输入安装区域',
      //     icon: 'none'
      //   })
      // } 
      else if (this.data.monitor_appkey == '') {
        wx.showToast({
          title: '请输入AppKey',
          icon: 'none'
        })
      } else if (this.data.monitor_appsecret == '') {
        wx.showToast({
          title: '请输入Secret',
          icon: 'none'
        })
      } else {
        this.pzsubmit()
      }
    }
    // 乐橙
    if (this.data.factoryid == 1) {
      // if (this.data.title == '') {
      //   wx.showToast({
      //     title: '请输入安装区域',
      //     icon: 'none'
      //   })
      // } else 
      if (this.data.monitor_id == '') {
        wx.showToast({
          title: '请输入设备编号',
          icon: 'none'
        })
      } else if (this.data.monitor_code == '') {
        wx.showToast({
          title: '请输入设备验证码',
          icon: 'none'
        })
      } else {
        this.pzsubmit()
      }
    }
  },
  pzsubmit() {
    let that = this
    that.setData({
      buttonClicked: true
    })
    var url = '';
    var data = {}
    // 1.乐橙，=3 萤石云
    // if (this.data.factoryid == 3) {
    //   url = 'Monitor/jhxc_shopAddMonitor'
    //   data = {
    //     shopId: wx.getStorageSync('shopId'),
    //     mid: that.data.mid, //服务商id
    //     title: '',
    //     // title: that.data.title,
    //     monitor_appkey: that.data.monitor_appkey, //AppKey
    //     monitor_appsecret: that.data.monitor_appsecret, //Secret
    //     monitor_id: that.data.monitor_id, //设备编号
    //     monitor_code: that.data.monitor_code, //设备验证码
    //     thumb: that.data.wudibu_img, //底部照片
    //   }
    // }
    // if (this.data.factoryid == 1) {
    //   url = 'Monitor/jhxc_lecheng_addMonitor'
    //   data = {
    //     shopId: wx.getStorageSync('shopId'),
    //     factoryid: 1, //服务商id
    //     // title: that.data.title,
    //     title: '',
    //     monitor_id: that.data.monitor_id, //设备编号
    //     monitor_code: that.data.monitor_code, //设备验证码
    //     remark: that.data.beizhu,
    //     monitor_appkey: that.data.monitor_appkey, //AppKey
    //     monitor_appsecret: that.data.monitor_appsecret, //Secret
    //   }
    // }
    this.myRequest(yuming + 'Monitor/monitoring_create_byshopId', {
      shopId:wx.getStorageSync('shopId'),
      mid:that.data.mid, //服务商id
      monitor_id:that.data.monitor_id, //设备编号
      monitor_code:that.data.monitor_code, //设备验证码
      monitor_appkey:that.data.monitor_appkey, //AppKey
      monitor_appsecret:that.data.monitor_appsecret,
      remark:that.data.beizhu,
      thumb:that.data.wudibu_img
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  scan() {
    this.jiankongimage(1).then(res => {
      const tempFilePaths = res.tempFilePaths
      this.myUploadImg(tempFilePaths[0], 'file').then(res => {
        var data = JSON.parse(res.data)
        this.getjkxx(data.imgUrl)
      })
    })
  },
  getjkxx(imgUrl) {
    this.myRequest(yuming + 'Besall/monitor_zhuan_text', {
      imgUrl
    }, 'POST').then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        this.setData({
          sbbh: res.data.data.SN,
          sbyz: res.data.data.SC
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      id: options.id
    })
    this.fuwuist()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      buttonClicked: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})